<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="产品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="产品状态" prop="goodsStatus">
        <el-select v-model="queryParams.goodsStatus" placeholder="请选择产品状态" clearable size="small">
          <el-option label="正常" :value="1" />
          <el-option label="限时抢购" :value="2" />
          <el-option label="拼团" :value="3" />
          <el-option label="下架" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:goods:add']"
        >新增产品</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:goods:remove']"
        >删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品编号" align="center" prop="goodsNumber" />
      <el-table-column label="产品封面" align="center" prop="goodsCover">
        <template slot-scope="scope">
          <el-image
            style="width:40px;height:40px"
            fit="cover"
            :src="scope.row.goodsCover[0]"
            :preview-src-list="scope.row.goodsCover"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="产品名称" align="center" prop="goodsName" />
      <el-table-column label="产品价格" align="center" prop="goodsMoneyMin">
        <template slot-scope="scope">
          <span>{{scope.row.goodsMoneyMin}}元~{{scope.row.goodsMoneyMax}}元</span>
        </template>
      </el-table-column>
      <el-table-column label="产品状态" align="center" prop="goodsStatus">
        <template slot-scope="scope">
          <span>{{scope.row.goodsStatus | filterGoodsStatus}}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.goodsStatus === 4"
            size="mini"
            type="text"
            style="color:#67C23A"
            @click="handleUpdateStatus(scope.row,1)"
            v-hasPermi="['system:goods:update']"
          >上架</el-button>

          <el-button
            v-if="scope.row.goodsStatus !== 4"
            size="mini"
            type="text"
            style="color:#F56C6C"
            @click="handleUpdateStatus(scope.row,4)"
            v-hasPermi="['system:goods:update']"
          >下架</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:goods:edit']"
          >编辑</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            class="delete-btn"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:goods:remove']"
          >删除</el-button>
          <router-link :to="{name: 'goodsEva', query: {id: scope.row.id, name: scope.row.goodsName}}" style="margin-left: 10px">
            <el-button
              size="mini"
              type="text"
            >评价</el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listGoods, delGoods, updateGoods } from "@/api/system/goods";

export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      goodsNumbers: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品表格数据
      goodsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsNumber: null,
        goodsName: null,
        goodsMoneyMin: null,
        goodsMoneyMax: null,
        goodsStatus: null,
        goodsCover: null,
        goodsAgreement: null,
        goodsContent: null,
        goodsStandard: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  filters: {
    filterGoodsStatus(value) {
      return {
        1: "正常",
        2: "限时抢购",
        3: "拼团",
        4: "下架"
      }[value];
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows.map(item => {
          item.goodsCover = item.goodsCover.split(",");
          return item;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.goodsNumbers = selection.map(item => item.goodsNumber);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        name: "GoodsEdit"
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        name: "GoodsEdit",
        query: {
          id: row.id
        }
      });
    },

    /** 上架，下架 */
    handleUpdateStatus(row, goodsStatus) {
      const ids = row.goodsNumber;
      this.$confirm(
        `是否确认${
          row.goodsStatus === 4 ? "上架" : "下架"
        }产品编号为${ids}的数据项?`,
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          return updateGoods({
            ...row,
            goodsAgreement: this.filterContent(row.goodsAgreement),
            goodsContent: this.filterContent(row.goodsContent),
            goodsCover: JSON.stringify(row.goodsCover),
            goodsStatus
          });
        })
        .then(() => {
          this.getList();
          this.msgSuccess(`${row.goodsStatus === 4 ? "上架" : "下架"}成功`);
        })
        .catch(function() {});
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const goodsNumbers = row.goodsNumber || this.goodsNumbers;

      this.$confirm(
        '是否确认删除产品编号为"' + goodsNumbers + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delGoods(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    }
  }
};
</script>
